package com.xe.gae.admin;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.blobstore.BlobInfo;
import com.google.appengine.api.blobstore.BlobInfoFactory;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.datastore.Text;
import com.xe.gae.XEngine;
import com.xe.gae.control.XEBlobstoreService;
import com.xe.gae.control.XEConfigManager;
import com.xe.gae.control.XEDataStorageService;
import com.xe.gae.control.XEResourcesManager;
import com.xe.gae.model.XEResource;
import com.xe.util.XEntity;

public class XEAdminServlet extends HttpServlet {
    
	String version=null;
	
public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws IOException {

	if (version==null){
		XEConfigManager cm = new XEConfigManager();
		this.version = cm.get("version");
		if (version==null) {
			res.sendRedirect("/_xe/admin/install.jsp");
			return;
		}
	}
	
	String action=req.getParameter("action");
	
	if (action==null) {
		res.sendRedirect("/_xe/admin/browse.jsp");
	}
	else if (action.equalsIgnoreCase("delete")){
		String pblobKey=req.getParameter("blob-key");
		if (pblobKey!=null){
			XEBlobstoreService bs = new XEBlobstoreService();
			BlobKey blobKey = new BlobKey(pblobKey);
			bs.delete(blobKey);
		}
		
		String  resourceKey = req.getParameter("resource-key");
		if (resourceKey!=null){
			XEDataStorageService ds = new XEDataStorageService();
			ds.delete(resourceKey);
		}
		
		res.sendRedirect("/_xe/admin/browse.jsp");
	}
	else if (action.equalsIgnoreCase("serve")){
		XEBlobstoreService bs = new XEBlobstoreService();
		BlobKey blobKey = new BlobKey(req.getParameter("blob-key"));
        
        BlobInfoFactory bi = new BlobInfoFactory();
        String fname = bi.loadBlobInfo(blobKey).getFilename();
        res.setContentType("application/x-download");
        res.setHeader("Content-Disposition", "attachment; filename=" + fname);
        
        bs.serve(blobKey, res);
        
	}
	else if (action.equalsIgnoreCase("new-folder")){
		String  resourceKey = req.getParameter("resource-key");
		String name= req.getParameter("name");
				
		XEResourcesManager rm = new XEResourcesManager();
		XEResource parent= rm.findByEncodedKey(resourceKey);
		rm.newFolder(parent, name);
		
		res.sendRedirect("/_xe/admin/browse.jsp");
	}
	
	else if (action.equalsIgnoreCase("run")){
		String  resourceKey = req.getParameter("resource-key");

		XEResourcesManager rm = new XEResourcesManager();
		
		XEResource resource = (XEResource)rm.findByEncodedKey(resourceKey);
		
		XEngine engine = new XEngine();	
		engine.run(resource.getPath(), req, res);
	}
	
	else
		res.sendRedirect("/_xe/admin/browse.jsp");
   }

@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub

		String action=req.getParameter("action");
		if (action.equalsIgnoreCase("save")){
			String  resourceKey = req.getParameter("resource-key");
			XEResourcesManager rm = new XEResourcesManager();
			XEResource resource= rm.findByEncodedKey(resourceKey);
			String source= req.getParameter("code");
			resource.setSource(new Text(source));
			XEDataStorageService dss = new XEDataStorageService<>();
			dss.put(resource);
		}
		else{
			this.doGet(req, resp);
		}
			
	}

@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		XEConfigManager cm = new XEConfigManager();
		this.version = cm.get("version");
		
		super.init();
	}
}

